// Copyright (c) MatrixFramework. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Collections.Generic;

namespace MatrixFramework.Keycloak.Abstractions.Models;

/// <summary>
/// Realm信息
/// </summary>
public class RealmInfo
{
    /// <summary>
    /// Realm名称
    /// </summary>
    public string Realm { get; set; } = string.Empty;

    /// <summary>
    /// 显示名称
    /// </summary>
    public string? DisplayName { get; set; }

    /// <summary>
    /// 是否启用
    /// </summary>
    public bool Enabled { get; set; }

    /// <summary>
    /// 描述
    /// </summary>
    public string? Description { get; set; }

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime? CreatedAt { get; set; }

    /// <summary>
    /// 最后修改时间
    /// </summary>
    public DateTime? LastModified { get; set; }

    /// <summary>
    /// 主题配置
    /// </summary>
    public ThemeConfiguration? Theme { get; set; }

    /// <summary>
    /// 安全配置
    /// </summary>
    public SecurityConfiguration? Security { get; set; }

    /// <summary>
    /// 令牌配置
    /// </summary>
    public TokenConfiguration? Tokens { get; set; }

    /// <summary>
    /// 附加属性
    /// </summary>
    public Dictionary<string, object> Attributes { get; set; } = new();

    /// <summary>
    /// 用户数量
    /// </summary>
    public int UsersCount { get; set; }

    /// <summary>
    /// 客户端数量
    /// </summary>
    public int ClientsCount { get; set; }

    /// <summary>
    /// 角色数量
    /// </summary>
    public int RolesCount { get; set; }
}

/// <summary>
/// 用户信息
/// </summary>
public class UserInfo
{
    /// <summary>
    /// 用户ID
    /// </summary>
    public string Id { get; set; } = string.Empty;

    /// <summary>
    /// 用户名
    /// </summary>
    public string Username { get; set; } = string.Empty;

    /// <summary>
    /// 邮箱
    /// </summary>
    public string Email { get; set; } = string.Empty;

    /// <summary>
    /// 名
    /// </summary>
    public string FirstName { get; set; } = string.Empty;

    /// <summary>
    /// 姓
    /// </summary>
    public string LastName { get; set; } = string.Empty;

    /// <summary>
    /// 是否启用
    /// </summary>
    public bool Enabled { get; set; }

    /// <summary>
    /// 邮箱是否已验证
    /// </summary>
    public bool EmailVerified { get; set; }

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime? CreatedTimestamp { get; set; }

    /// <summary>
    /// 最后修改时间
    /// </summary>
    public DateTime? ModifiedTimestamp { get; set; }

    /// <summary>
    /// 用户角色列表
    /// </summary>
    public List<string> Roles { get; set; } = new();

    /// <summary>
    /// 用户组列表
    /// </summary>
    public List<string> Groups { get; set; } = new();

    /// <summary>
    /// 附加属性
    /// </summary>
    public Dictionary<string, object> Attributes { get; set; } = new();

    /// <summary>
    /// 租户ID（多租户场景）
    /// </summary>
    public string? TenantId { get; set; }

    /// <summary>
    /// 全名
    /// </summary>
    public string FullName => $"{FirstName} {LastName}".Trim();

    /// <summary>
    /// 显示名称
    /// </summary>
    public string DisplayName => !string.IsNullOrEmpty(FullName) ? FullName : Username;
}

/// <summary>
/// 角色信息
/// </summary>
public class RoleInfo
{
    /// <summary>
    /// 角色ID
    /// </summary>
    public string Id { get; set; } = string.Empty;

    /// <summary>
    /// 角色名称
    /// </summary>
    public string Name { get; set; } = string.Empty;

    /// <summary>
    /// 角色描述
    /// </summary>
    public string? Description { get; set; }

    /// <summary>
    /// 是否为复合角色
    /// </summary>
    public bool Composite { get; set; }

    /// <summary>
    /// 是否为客户端角色
    /// </summary>
    public bool ClientRole { get; set; }

    /// <summary>
    /// 容器ID
    /// </summary>
    public string? ContainerId { get; set; }

    /// <summary>
    /// 客户端ID
    /// </summary>
    public string? ClientId { get; set; }

    /// <summary>
    /// 关联的角色列表
    /// </summary>
    public List<RoleInfo> Composites { get; set; } = new();

    /// <summary>
    /// 附加属性
    /// </summary>
    public Dictionary<string, object> Attributes { get; set; } = new();
}

/// <summary>
/// 客户端信息
/// </summary>
public class ClientInfo
{
    /// <summary>
    /// 客户端ID
    /// </summary>
    public string Id { get; set; } = string.Empty;

    /// <summary>
    /// 客户端UUID
    /// </summary>
    public string ClientId { get; set; } = string.Empty;

    /// <summary>
    /// 客户端名称
    /// </summary>
    public string Name { get; set; } = string.Empty;

    /// <summary>
    /// 客户端协议
    /// </summary>
    public string Protocol { get; set; } = string.Empty;

    /// <summary>
    /// 访问类型
    /// </summary>
    public ClientAccessType AccessType { get; set; }

    /// <summary>
    /// 是否启用
    /// </summary>
    public bool Enabled { get; set; }

    /// <summary>
    /// 是否公开客户端
    /// </summary>
    public bool PublicClient { get; set; }

    /// <summary>
    /// 是否标准流已启用
    /// </summary>
    public bool StandardFlowEnabled { get; set; }

    /// <summary>
    /// 是否隐式流已启用
    /// </summary>
    public bool ImplicitFlowEnabled { get; set; }

    /// <summary>
    /// 是否直接访问已启用
    /// </summary>
    public bool DirectAccessGrantsEnabled { get; set; }

    /// <summary>
    /// 是否服务账户已启用
    /// </summary>
    public bool ServiceAccountsEnabled { get; set; }

    /// <summary>
    /// 重定向URI列表
    /// </summary>
    public List<string> RedirectUris { get; set; } = new();

    /// <summary>
    /// 允许的来源
    /// </summary>
    public List<string> WebOrigins { get; set; } = new();

    /// <summary>
    /// 默认角色
    /// </summary>
    public List<string> DefaultRoles { get; set; } = new();

    /// <summary>
    /// 授权设置
    /// </summary>
    public AuthorizationSettings? Authorization { get; set; }

    /// <summary>
    /// 附加属性
    /// </summary>
    public Dictionary<string, object> Attributes { get; set; } = new();

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime? CreatedAt { get; set; }

    /// <summary>
    /// 最后修改时间
    /// </summary>
    public DateTime? ModifiedAt { get; set; }
}

/// <summary>
/// 权限作用域信息
/// </summary>
public class ScopeInfo
{
    /// <summary>
    /// 作用域ID
    /// </summary>
    public string Id { get; set; } = string.Empty;

    /// <summary>
    /// 作用域名称
    /// </summary>
    public string Name { get; set; } = string.Empty;

    /// <summary>
    /// 显示名称
    /// </summary>
    public string? DisplayName { get; set; }

    /// <summary>
    /// 描述
    /// </summary>
    public string? Description { get; set; }

    /// <summary>
    /// 图标URI
    /// </summary>
    public string? IconUri { get; set; }

    /// <summary>
    /// 客户端ID
    /// </summary>
    public string? ClientId { get; set; }

    /// <summary>
    /// 协议
    /// </summary>
    public string Protocol { get; set; } = string.Empty;

    /// <summary>
    /// 包含的协议映射器
    /// </summary>
    public List<ProtocolMapperInfo> ProtocolMappers { get; set; } = new();

    /// <summary>
    /// 包含的角色
    /// </summary>
    public List<RoleInfo> Roles { get; set; } = new();

    /// <summary>
    /// 附加属性
    /// </summary>
    public Dictionary<string, object> Attributes { get; set; } = new();
}

/// <summary>
/// 协议映射器信息
/// </summary>
public class ProtocolMapperInfo
{
    /// <summary>
    /// 映射器ID
    /// </summary>
    public string Id { get; set; } = string.Empty;

    /// <summary>
    /// 映射器名称
    /// </summary>
    public string Name { get; set; } = string.Empty;

    /// <summary>
    /// 协议
    /// </summary>
    public string Protocol { get; set; } = string.Empty;

    /// <summary>
    /// 协议映射器
    /// </summary>
    public string ProtocolMapper { get; set; } = string.Empty;

    /// <summary>
    /// 配置
    /// </summary>
    public Dictionary<string, object> Config { get; set; } = new();
}

/// <summary>
/// 服务器信息
/// </summary>
public class ServerInfo
{
    /// <summary>
    /// 服务器版本
    /// </summary>
    public string Version { get; set; } = string.Empty;

    /// <summary>
    /// 构建时间
    /// </summary>
    public DateTime? BuildTime { get; set; }

    /// <summary>
    /// Git提交ID
    /// </summary>
    public string? GitCommitId { get; set; }

    /// <summary>
    /// 系统信息
    /// </summary>
    public SystemInfo? SystemInfo { get; set; }

    /// <summary>
    /// 内存信息
    /// </summary>
    public MemoryInfo? MemoryInfo { get; set; }

    /// <summary>
    /// 功能列表
    /// </summary>
    public List<string> Features { get; set; } = new();

    /// <summary>
    /// 配置信息
    /// </summary>
    public Dictionary<string, object> Configuration { get; set; } = new();
}

/// <summary>
/// 系统信息
/// </summary>
public class SystemInfo
{
    /// <summary>
    /// Java版本
    /// </summary>
    public string? JavaVersion { get; set; }

    /// <summary>
    /// Java供应商
    /// </summary>
    public string? JavaVendor { get; set; }

    /// <summary>
    /// 操作系统名称
    /// </summary>
    public string? OsName { get; set; }

    /// <summary>
    /// 操作系统版本
    /// </summary>
    public string? OsVersion { get; set; }

    /// <summary>
    /// 操作系统架构
    /// </summary>
    public string? OsArchitecture { get; set; }

    /// <summary>
    /// 主机名
    /// </summary>
    public string? HostName { get; set; }

    /// <summary>
    /// CPU核心数
    /// </summary>
    public int? CpuCores { get; set; }
}

/// <summary>
/// 内存信息
/// </summary>
public class MemoryInfo
{
    /// <summary>
    /// 最大内存（字节）
    /// </summary>
    public long? MaxMemory { get; set; }

    /// <summary>
    /// 总内存（字节）
    /// </summary>
    public long? TotalMemory { get; set; }

    /// <summary>
    /// 可用内存（字节）
    /// </summary>
    public long? FreeMemory { get; set; }

    /// <summary>
    /// 已用内存（字节）
    /// </summary>
    public long? UsedMemory { get; set; }

    /// <summary>
    /// 内存使用百分比
    /// </summary>
    public double? MemoryUsagePercentage => TotalMemory.HasValue && UsedMemory.HasValue
        ? (double)UsedMemory.Value / TotalMemory.Value * 100
        : null;
}

/// <summary>
/// 连接信息
/// </summary>
public class ConnectionInfo
{
    /// <summary>
    /// 连接URL
    /// </summary>
    public string Url { get; set; } = string.Empty;

    /// <summary>
    /// 响应时间（毫秒）
    /// </summary>
    public long ResponseTimeMs { get; set; }

    /// <summary>
    /// HTTP状态码
    /// </summary>
    public int StatusCode { get; set; }

    /// <summary>
    /// 是否成功
    /// </summary>
    public bool Success { get; set; }

    /// <summary>
    /// 错误消息
    /// </summary>
    public string? ErrorMessage { get; set; }

    /// <summary>
    /// 连接时间
    /// </summary>
    public DateTime ConnectedAt { get; set; }

    /// <summary>
    /// TLS版本
    /// </summary>
    public string? TlsVersion { get; set; }

    /// <summary>
    /// 认证方式
    /// </summary>
    public string? AuthenticationMethod { get; set; }

    /// <summary>
    /// 附加信息
    /// </summary>
    public Dictionary<string, object> AdditionalInfo { get; set; } = new();
}